package com.aki.ai_property.service.impl;

import com.aki.ai_property.dao.ContractDao;
import com.aki.ai_property.entity.Contract;
import com.aki.ai_property.service.intf.ContractService;
import com.aki.ai_property.util.StringUtil;
import com.aki.ai_property.vo.PageBean;
import com.aki.ai_property.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 孙贺鹏
 * @date 2022/8/27
 */
@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    private ContractDao dao;
    @Override
    public Result save(Contract contract) {
        if (dao.save(contract)>0){
            return Result.ok("新增成功");
        }else {
            return Result.ok("新增失败");
        }
    }

    @Override
    public Result update(Contract contract) {
        if (dao.update(contract) >0){
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }

    @Override
    public Result delete(int id) {
        if (dao.delete(id) >0){
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    @Override
    public PageBean selectPage(String contractName, int page, int limit) {
        if (StringUtil.isNotEmpty(contractName)){
            contractName = "%"+contractName+"%";
        }
        int start = (page - 1) * limit;
        return PageBean.ok(dao.selectCount(contractName), dao.selectPage(contractName, start, limit));
    }

    /** 查询全部*/
    @Override
    public Result all() {
        return Result.ok(dao.all());
    }
}
